package com.br.planningrun.repositorio;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.br.planningrun.entidade.PlanoCorrida;

public class RepositorioPlanoCorrida {
	// Atributos
	private Context context;
	private SQLiteDatabase db2 = null;
	private SQLiteStatement stmt = null;
	private Cursor c = null;
	
	private static final String NOME_TABELA = "tb_PlanoCorrida";
	private static final String BANCO = "planningrun";
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	// Construtor
	public RepositorioPlanoCorrida(Context context){
		this.context = context;
	}

	
	// Inserir objeto
	public void inserir(PlanoCorrida PlanoCorrida){
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);

			stmt = db2.compileStatement("INSERT INTO " 
					+ NOME_TABELA + " (codigo,titulo,descricao, personalizado) VALUES (?,?,?,?)");
			
			stmt.bindLong(1, PlanoCorrida.getCodigo());
			stmt.bindString(2, PlanoCorrida.getTitulo());
			stmt.bindString(3, PlanoCorrida.getDescricao());
			stmt.bindString(4, PlanoCorrida.getPersonalizado());

			stmt.execute();
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
	}

	// Atualizacao do objeto
	public void atualizar(PlanoCorrida PlanoCorrida) {
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);

			stmt = db2.compileStatement("UPDATE " + NOME_TABELA 
					+ " SET " + "titulo = ?, descricao = ?, personalizado = ?  WHERE codigo = ?;");

			stmt.bindString(1, PlanoCorrida.getTitulo());
			stmt.bindString(2, PlanoCorrida.getDescricao());
			stmt.bindString(3, PlanoCorrida.getPersonalizado());
			stmt.bindLong(4, PlanoCorrida.getCodigo());

			
			stmt.execute();
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
	}


	// Remover objeto (CODIGO)
	public void remover(int codigo) {
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);

			stmt = db2.compileStatement("DELETE FROM " 
					+ NOME_TABELA + " WHERE codigo = ?;");

			stmt.bindLong(1, codigo);

			stmt.execute();
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
	}

	// Remover objeto (WHERE)
	public void remover(String where) {
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);

			stmt = db2.compileStatement("DELETE FROM " 
					+ NOME_TABELA + " " + where);

			stmt.execute();
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
	}

	// Consulta do objeto (CODIGO)
	public PlanoCorrida procurar(int codigo) {
		ArrayList<PlanoCorrida> lista = this.listar(" WHERE codigo = " + codigo);
		PlanoCorrida PlanoCorrida = null;
		if (lista.size() > 0) {
			PlanoCorrida = lista.get(0);
		}
		return PlanoCorrida;
	}

	public int getMaxId() {
		int retorno = 0;
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);
			c = db2.rawQuery("SELECT MAX(codigo) as maximo FROM " + NOME_TABELA + ";",
					null);

			c.moveToNext();
			retorno = c.getInt(c.getColumnIndex("maximo"));
			
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
		
		return retorno;

	}

	
	// Listagem dos objetos
	public ArrayList<PlanoCorrida> listar(String where) {
		ArrayList<PlanoCorrida> lista = new ArrayList<PlanoCorrida>();
		
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);
			c = db2.rawQuery("SELECT * FROM " + NOME_TABELA + " " + where + ";",
					null);

			while (c.moveToNext()) {
				PlanoCorrida PlanoCorrida = new PlanoCorrida();
				PlanoCorrida.setCodigo(c.getInt(c.getColumnIndex("codigo")));
				PlanoCorrida.setTitulo(c.getString(c.getColumnIndex("titulo")));
				PlanoCorrida.setPersonalizado(c.getString(c.getColumnIndex("personalizado")));
				PlanoCorrida.setDescricao(c.getString(c.getColumnIndex("descricao")));

				lista.add(PlanoCorrida);
			}
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
		
		return lista;
	}

	// Criacao da tabela
	public void truncate() {
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);
			// Exclusao da tabela se existir
			db2.execSQL("DROP TABLE IF EXISTS " + NOME_TABELA);
			// Criacao da tabela
			db2.execSQL("CREATE TABLE IF NOT EXISTS " + NOME_TABELA + "( codigo INTEGER NOT NULL PRIMARY KEY,"
					+ "titulo varchar2(255) ,"
					+" descricao varchar2(255),"
					+" personalizado varchar2(255)  );");
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
	}
}
